package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TabChatMsg;
import com.ruoyi.system.service.ITabChatMsgService;
import com.ruoyi.system.service.impl.RedisService;
import com.ruoyi.system.service.impl.WxService;
import com.ruoyi.system.service.impl.XfService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author：zhanghao
 * @name：WxMsgTask
 * @Date：2024/7/18 14:44
 */
@Component("wxMsgTask")
public class WxMsgTask {

    protected final Logger logger = LoggerFactory.getLogger(WxMsgTask.class);

    @Value("${wx.sys.wxid}")
    private String wxid;
    @Autowired
    private ITabChatMsgService tabChatMsgService;
    @Autowired
    private XfService xfService;
    @Autowired
    private WxService wxService;
    @Autowired
    private RedisService redisService;

    private String wxdlk = "wx:msg";


    public void replyMsg() {
//        System.out.println(DateUtil.format(new Date(),"yyyy-MM-dd HH:MM:ss")+"执行方法：replyMsg," + wxid);
        getRedisMsg();

    }

    private void getRedisMsg() {
        String json = (String) redisService.rPop(wxdlk);
        if (StringUtils.isNotEmpty(json)) {
            TabChatMsg m = JSONUtil.toBean(json, TabChatMsg.class);

            if (m.getFromUser().indexOf("@chatroom") == -1 && !m.getFromUser().equals("weixin")) {
                try {
                    String info = xfService.sendxfai(m.getContent());
                    System.out.println("info=" + info);

                    ObjectMapper om = new ObjectMapper();
                    Map map = om.readValue(info, Map.class);
                    String code = null != map.get("code") ? map.get("code").toString() : "";
//                    String message = null != map.get("message") ? map.get("message").toString() : "";
                    String text = null;
                    if (StringUtils.equals("0", code)) {
                        List<Map> mapList = (List<Map>) map.get("choices");
                        Map choicesMap = mapList.get(0);
                        Map msgMap = (Map) choicesMap.get("message");
                        text = msgMap.get("content").toString();
                    }

                    info = wxService.sendTextMsg(m.getFromUser(), text);
                    logger.info("result=" + info);
                    logger.info("消息处理完成{}", json);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("消息处理异常{}", json);
                }
            } else {
                logger.warn("消息不处理{}", json);
            }


        }

    }

    private void getSqlMsg() {
        TabChatMsg msg = new TabChatMsg();
        msg.setToUser(wxid);
        msg.setState(0l);
        List<TabChatMsg> msgList = tabChatMsgService.selectTabChatMsgList(msg);
        if (null != msgList && msgList.size() > 0) {
            for (TabChatMsg m : msgList) {
                if (m.getFromUser().indexOf("@chatroom") == -1 && !m.getFromUser().equals("weixin")) {

                    try {
                        String info = xfService.sendxfai(m.getContent());
                        System.out.println("info=" + info);

                        ObjectMapper om = new ObjectMapper();
                        Map map = om.readValue(info, Map.class);
                        String code = null != map.get("code") ? map.get("code").toString() : "";
                        String message = null != map.get("message") ? map.get("message").toString() : "";
                        String text = null;
                        if (StringUtils.equals("0", code)) {
                            List<Map> mapList = (List<Map>) map.get("choices");
                            Map choicesMap = mapList.get(0);
                            Map msgMap = (Map) choicesMap.get("message");
                            text = msgMap.get("content").toString();
                        }

                        info = wxService.sendTextMsg(m.getFromUser(), text);
                        System.out.println("result=" + info);
                        tabChatMsgService.updateTabChatMsg(m.setState(1l));
                    } catch (Exception e) {
                        e.printStackTrace();
                        tabChatMsgService.updateTabChatMsg(m.setState(2l));
                    }
                } else {
                    tabChatMsgService.updateTabChatMsg(m.setState(1l));
                }

            }
        }
    }


}
